package com.empire.module.sporttery.util;

import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.StringUtils;
import com.empire.module.sporttery.vo.OddsVO;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 竞猜相关的工具
 */
public class SportteryUtils {

    /**
     * 获取查询的时间格式
     *
     * @param pageNo
     * @param pageSize
     * @return List<String>
     */
    public static List<String> getFootballStartTime(int pageNo, int pageSize, String startTime) {
        //2025-02-12
        Map<Integer, String> w = new HashMap<>();
        w.put(1, "星期天");
        w.put(2, "星期一");
        w.put(3, "星期二");
        w.put(4, "星期三");
        w.put(5, "星期四");
        w.put(6, "星期五");
        w.put(7, "星期六");
        Date start = new Date();
        if (StringUtils.isNotBlank(startTime)) {
            // Parse the input date and the reference date
            LocalDateTime inputDate = toLocalDateTime(startTime).atStartOfDay();
            LocalDateTime referenceDate = toLocalDateTime("2025-02-22").atStartOfDay();

            // Only set the start date if input date is after reference date
            if (inputDate.isAfter(referenceDate)) {
                start = Date.from(inputDate.atZone(ZoneId.systemDefault()).toInstant());
            }
        }
        // 查询字段内容的数据格式转换
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start); // 设置当前日期
        //如果是第二页以上，需要统一减去
        if (pageNo > 1) {
            calendar.add(Calendar.DAY_OF_MONTH, -(pageNo - 1) * pageSize); // 翻页处理
        }
        List<String> days = new ArrayList<>();
        int i = 0;
        while (i < pageSize) {
            if (i > 0) {
                calendar.add(Calendar.DAY_OF_MONTH, -1); // 往前i天
            }
            String d = DateUtils.format(calendar.getTime(), DateUtils.DATE_FORMAT_10);//格式化日期
            //获取当前日期是一周中的第几天，注意Java中周日是一周的第一天，值为1，周一为2，依此类推
            int workDay = calendar.get(Calendar.DAY_OF_WEEK);
            days.add(String.format("%s %s", d, w.get(workDay)));
            i++;
        }
        return days;
    }

    /**
     * String转 LocalDateTime
     *
     * @param dateString 日期的字符串
     * @return
     */
    public static LocalDate toLocalDateTime(String dateString) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 定义日期格式
        return LocalDate.parse(dateString, formatter);
    }

    /**
     * 根据字符串获取胜平负的赔率
     *
     * @param oods
     * @return
     */
    public static List<OddsVO> getSPFOddsVO(String oods) {
        List<OddsVO> list = new ArrayList<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(oods)) {
            String[] os = oods.split(",");
            OddsVO s = new OddsVO(Double.parseDouble(os[0]), 0, 1);
            list.add(s);
            OddsVO p = new OddsVO(Double.parseDouble(os[1]), 0, 1);
            list.add(p);
            OddsVO f = new OddsVO(Double.parseDouble(os[2]), 0, 1);
            list.add(f);
        }
        return list;
    }

    /**
     * 根据字符串获取半全场的赔率
     *
     * @param oods
     * @return
     */
    public static List<OddsVO> getHHDFOddsVO(String oods) {
        List<OddsVO> list = new ArrayList<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(oods)) {
            String[] os = oods.split(",");
            OddsVO ss = new OddsVO(Double.parseDouble(os[0]), 0, 1);
            list.add(ss);
            OddsVO sp = new OddsVO(Double.parseDouble(os[1]), 0, 1);
            list.add(sp);
            OddsVO sf = new OddsVO(Double.parseDouble(os[2]), 0, 1);
            list.add(sf);
            OddsVO ps = new OddsVO(Double.parseDouble(os[3]), 0, 1);
            list.add(ps);
            OddsVO pp = new OddsVO(Double.parseDouble(os[4]), 0, 1);
            list.add(pp);
            OddsVO pf = new OddsVO(Double.parseDouble(os[5]), 0, 1);
            list.add(pf);
            OddsVO fs = new OddsVO(Double.parseDouble(os[6]), 0, 1);
            list.add(fs);
            OddsVO fp = new OddsVO(Double.parseDouble(os[7]), 0, 1);
            list.add(fp);
            OddsVO ff = new OddsVO(Double.parseDouble(os[8]), 0, 1);
            list.add(ff);

        }
        return list;
    }

    /**
     * 根据字符串获取总进球的赔率
     *
     * @param oods
     * @return
     */
    public static List<OddsVO> getTTGOddsVO(String oods) {
        List<OddsVO> list = new ArrayList<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(oods)) {
            String[] os = oods.split(",");
            OddsVO s0 = new OddsVO(Double.parseDouble(os[0]), 0, 1);
            list.add(s0);
            OddsVO s1 = new OddsVO(Double.parseDouble(os[1]), 0, 1);
            list.add(s1);
            OddsVO s2 = new OddsVO(Double.parseDouble(os[2]), 0, 1);
            list.add(s2);
            OddsVO s3 = new OddsVO(Double.parseDouble(os[3]), 0, 1);
            list.add(s3);
            OddsVO s4 = new OddsVO(Double.parseDouble(os[4]), 0, 1);
            list.add(s4);
            OddsVO s5 = new OddsVO(Double.parseDouble(os[5]), 0, 1);
            list.add(s5);
            OddsVO s6 = new OddsVO(Double.parseDouble(os[6]), 0, 1);
            list.add(s6);
            OddsVO s7 = new OddsVO(Double.parseDouble(os[7]), 0, 1);
            list.add(s7);
        }
        return list;
    }
}
